package cn.tedu.egarticle0616.controller;

import cn.tedu.egarticle0616.base.response.JsonResult;
import cn.tedu.egarticle0616.base.response.StatusCode;
import cn.tedu.egarticle0616.mapper.ArticleMapper;
import cn.tedu.egarticle0616.mapper.UserMapper;
import cn.tedu.egarticle0616.pojo.datatransferobject.UserArticleParam;
import cn.tedu.egarticle0616.pojo.datatransferobject.UserLoginParam;
import cn.tedu.egarticle0616.pojo.entity.Article;
import cn.tedu.egarticle0616.pojo.valueobject.ArticleVO;
import cn.tedu.egarticle0616.pojo.valueobject.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Tag(name="01-用户模块")
@Slf4j
@Validated
@RestController
@RequestMapping("/v1/user/")
public class Controller {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ArticleMapper articleMapper;

    @PostMapping("login")
    @Operation(summary = "01-用户登录功能")
    @ApiOperationSupport(order = 10)
    public JsonResult loginParam(@RequestBody UserLoginParam userLoginParam, HttpSession session) {
        log.debug("userLoginParam:" + userLoginParam);
        UserVO userVO = userMapper.login(userLoginParam);
       if (userVO == null) {
          return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
       } else {
            session.setAttribute("user", userVO);
        }
        return JsonResult.correct(userVO);
    }

    @PostMapping("article")
    @Operation(summary = "02-发表文章功能")
    @ApiOperationSupport(order = 20)
    public JsonResult articleParam(@Validated @RequestBody UserArticleParam userArticleParam, HttpSession session) {
        log.debug("userArticleParam:" + userArticleParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
//        if (userVO == null) {
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        } else {
            Article article = new Article();
            BeanUtils.copyProperties(userArticleParam, article);
            article.setUserId(userVO.getId());
            log.debug("article:" + article);
            int num = articleMapper.addArticle(article);
            if (num > 0) {
                return JsonResult.correct();
            } else {
                return new JsonResult(StatusCode.OPERATION_FAILED);
            }
//        }
    }

    @GetMapping("selectByArticleId")
    @Operation(summary = "03-查询指定文章功能")
    @ApiOperationSupport(order = 30)
    @Parameter(name = "id", description = "文章id")
    public JsonResult selectById(@Range(min=1,max = 100,message = "文章id必须在1-100之间") Long id, HttpSession session) {
        log.debug("id:" + id);
        UserVO userVO = (UserVO) session.getAttribute("user");
//        if (userVO == null) {
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        } else {
            ArticleVO articleVO = articleMapper.query(id);
            if (articleVO == null) {
                return new JsonResult(StatusCode.OPERATION_FAILED);
            } else {
                return JsonResult.correct(articleVO);
            }
//        }
    }
}
